debugging
[RRRRHHHH_Code] / ruralHouses client / src / gui / ModifyOfferGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Rectangle;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.FocusListener;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.rmi.Naming;
15 import java.sql.Date;
16 import java.text.DateFormat;
17 import java.util.Calendar;
18 import java.util.Locale;
19 import java.util.Vector;
20
21 import javax.swing.JButton;
22 import javax.swing.JComboBox;
23 import javax.swing.JFrame;
24 import javax.swing.JLabel;
25 import javax.swing.JTextField;
26
27 import com.toedter.calendar.JCalendar;
28 import common.OfferInterface;
29
30 import configuration.___IntNames;
31 import domain.Offer;
32 import domain.RuralHouse;
33 import exceptions.BadDates;
34
35 public class ModifyOfferGUI extends JFrame {
36
37         private static final long serialVersionUID = 1L;
38
39         private JComboBox<RuralHouse> jComboBox1;
40         private JLabel jLabel1 = new JLabel();
41         private JLabel jLabel2 = new JLabel();
42         private JTextField jTextField1 = new JTextField();
43         private JLabel jLabel3 = new JLabel();
44         private JTextField jTextField2 = new JTextField();
45         private JLabel jLabel4 = new JLabel();
46         private JTextField jTextField3 = new JTextField();
47         private JButton jButton1 = new JButton();
48         // Code for JCalendar
49         private JCalendar jCalendar1 = new JCalendar();
50         private JCalendar jCalendar2 = new JCalendar();
51         private Calendar calendarInicio = null;
52         private Calendar calendarFin = null;
53         private JButton jButton2 = new JButton();
54         private JLabel jLabel5 = new JLabel();
55         private final JLabel jLabel1_o = new JLabel();
56         private JComboBox<Offer> comboBox_o;
57
58         public ModifyOfferGUI(Vector<RuralHouse> v) {
59                 try {
60                         jbInit(v);
61                 } catch (Exception e) {
62                         e.printStackTrace();
63                 }
64         }
65
66         private void jbInit(Vector<RuralHouse> v) throws Exception {
67                 this.getContentPane().setLayout(null);
68                 this.setSize(new Dimension(513, 433));
69                 this.setTitle("Set availability");
70
71                 OfferInterface  om = null;
72                 
73                 jComboBox1 = new JComboBox<RuralHouse>(v);
74
75                 comboBox_o = new JComboBox<Offer>(
76                                 ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
77                 DateFormat dateformat1 = DateFormat.getDateInstance(1,
78                                 jCalendar1.getLocale());
79                 if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
80                                 .isEmpty()) {
81                         jTextField1.setText(dateformat1.format(((Offer) comboBox_o
82                                         .getSelectedItem()).getFirstDay()));
83                         jTextField2.setText(dateformat1.format(((Offer) comboBox_o
84                                         .getSelectedItem()).getLastDay()));             
85                         jLabel4.setText(Float.toString(((Offer) comboBox_o
86                                         .getSelectedItem()).getPrice()));
87                         jTextField3.setText(Float.toString(((Offer) comboBox_o
88                                         .getSelectedItem()).getPrice()));
89                 } else {
90                         jLabel5.setText("There are no offers for the selected rural house");
91                         jCalendar1.setEnabled(false);
92                         jCalendar2.setEnabled(false);
93                         jButton1.setEnabled(false);
94                         comboBox_o.setEnabled(false);
95                         jTextField3.setEnabled(false);
96                 }
97                 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
98                 jLabel1.setText("List of houses:");
99                 jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
100                 jLabel2.setText("First day :");
101                 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
102                 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
103                 jTextField1.setEditable(false);
104                 jLabel3.setText("Last day :");
105                 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
106                 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
107                 jTextField2.setEditable(false);
108                 jLabel4.setText("Price:");
109                 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
110                 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
111                 jTextField3.setText("0");
112                 jButton1.setText("Accept");
113                 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
114                 jTextField3.addFocusListener(new FocusListener() {
115                         public void focusGained(FocusEvent e) {
116                         }
117
118                         public void focusLost(FocusEvent e) {
119                                 jTextField3_focusLost();
120                         }
121                 });
122
123                 jComboBox1.addItemListener(new ItemListener() {
124
125                         @Override
126                         public void itemStateChanged(ItemEvent arg0) {
127                                 Vector<Offer> vo = ((RuralHouse) jComboBox1.getSelectedItem()).offers;
128                                 comboBox_o.removeAllItems();
129                                 if (!((RuralHouse) jComboBox1.getSelectedItem()).offers
130                                                 .isEmpty()) {
131                                         jCalendar1.setEnabled(true);
132                                         jCalendar2.setEnabled(true);
133                                         jButton1.setEnabled(true);
134                                         comboBox_o.setEnabled(true);
135                                         jTextField3.setEnabled(true);
136                                         jLabel5.setText("");
137                                         for (Offer of : vo) {
138                                                 comboBox_o.addItem(of);
139                                         }
140                                 } else {
141                                         jLabel5.setText("There are no offers for the selected rural house");
142                                         jCalendar1.setEnabled(false);
143                                         jCalendar2.setEnabled(false);
144                                         jButton1.setEnabled(false);
145                                         comboBox_o.setEnabled(false);
146                                         jTextField3.setEnabled(false);
147                                         
148                                 }
149                         }
150
151                 });
152
153                 comboBox_o.addItemListener(new ItemListener() {
154
155                         @Override
156                         public void itemStateChanged(ItemEvent arg0) {
157                                 if (arg0.getStateChange() == ItemEvent.SELECTED) {
158                                         Offer of = (Offer) comboBox_o.getSelectedItem();
159                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
160                                                         jCalendar1.getLocale());
161                                         jTextField1.setText(dateformat1.format(of.getFirstDay()));
162                                         jTextField2.setText(dateformat1.format(of.getLastDay()));
163                                 }
164                         }
165
166                 });
167
168                 jButton1.addActionListener(new ActionListener() {
169                         public void actionPerformed(ActionEvent e) {
170                                 jButton1_actionPerformed(e);
171                         }
172                 });
173                 jButton2.setText("Cancel");
174                 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
175                 jButton2.addActionListener(new ActionListener() {
176                         public void actionPerformed(ActionEvent e) {
177                                 jButton2_actionPerformed(e);
178                         }
179                 });
180                 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
181                 jLabel5.setForeground(Color.red);
182                 jLabel5.setSize(new Dimension(305, 20));
183                 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
184                 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
185
186                 // Code for JCalendar
187                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
188                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
189                                 if (propertychangeevent.getPropertyName().equals("locale")) {
190                                         jCalendar1.setLocale((Locale) propertychangeevent
191                                                         .getNewValue());
192                                         DateFormat dateformat = DateFormat.getDateInstance(1,
193                                                         jCalendar1.getLocale());
194                                         jTextField1.setText(dateformat.format(calendarInicio
195                                                         .getTime()));
196                                 } else if (propertychangeevent.getPropertyName().equals(
197                                                 "calendar")) {
198                                         calendarInicio = (Calendar) propertychangeevent
199                                                         .getNewValue();
200                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
201                                                         jCalendar1.getLocale());
202                                         jTextField1.setText(dateformat1.format(calendarInicio
203                                                         .getTime()));
204                                         jCalendar1.setCalendar(calendarInicio);
205                                 }
206                         }
207                 });
208
209                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
210                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
211                                 if (propertychangeevent.getPropertyName().equals("locale")) {
212                                         jCalendar2.setLocale((Locale) propertychangeevent
213                                                         .getNewValue());
214                                         DateFormat dateformat = DateFormat.getDateInstance(1,
215                                                         jCalendar2.getLocale());
216                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
217                                 } else if (propertychangeevent.getPropertyName().equals(
218                                                 "calendar")) {
219                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
220                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
221                                                         jCalendar2.getLocale());
222                                         jTextField2.setText(dateformat1.format(calendarFin
223                                                         .getTime()));
224                                         jCalendar2.setCalendar(calendarFin);
225                                 }
226                         }
227                 });
228
229                 this.getContentPane().add(jCalendar2, null);
230                 this.getContentPane().add(jCalendar1, null);
231                 this.getContentPane().add(jLabel5, null);
232                 this.getContentPane().add(jButton2, null);
233                 this.getContentPane().add(jButton1, null);
234                 this.getContentPane().add(jTextField3, null);
235                 this.getContentPane().add(jLabel4, null);
236                 this.getContentPane().add(jTextField2, null);
237                 this.getContentPane().add(jLabel3, null);
238                 this.getContentPane().add(jTextField1, null);
239                 this.getContentPane().add(jLabel2, null);
240                 this.getContentPane().add(jLabel1, null);
241                 this.getContentPane().add(jComboBox1, null);
242                 jLabel1_o.setText("List of offers:");
243                 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
244                 jLabel1_o.setBounds(25, 44, 95, 20);
245
246                 getContentPane().add(jLabel1_o);
247                 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
248                 comboBox_o.setBounds(115, 44, 115, 20);
249
250                 getContentPane().add(comboBox_o);
251         }
252
253         private void jButton1_actionPerformed(ActionEvent e) {
254                 RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
255                 Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
256                 // Remove the hour:minute:second:ms from the date
257                 firstDay = Date.valueOf(firstDay.toString());
258                 Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
259                 // Remove the hour:minute:second:ms from the date
260                 lastDay = Date.valueOf(lastDay.toString());
261                 
262
263                 try {
264
265                         // It could be to trigger an exception if the introduced string is
266                         // not a number
267                         float price = Float.parseFloat(jTextField3.getText());
268
269                         // Obtain the business logic from a StartWindow class (local or
270                         // remote)
271                         OfferInterface offerM = null;
272                         try {
273                                 offerM = (OfferInterface) Naming
274                                                 .lookup(___IntNames.OfferManager);
275                         } catch (Exception e1) {
276                                 System.out.println("Error accessing remote authentication: "
277                                                 + e1.toString());
278                         }
279                         
280                         offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
281                         offerM.createOffer(ruralHouse, firstDay, lastDay, price);
282
283                         jLabel5.setText("Offer modified");
284
285                 } catch (java.lang.NumberFormatException e1) {
286                         jLabel5.setText(jTextField3.getText() + " is not a valid price");
287                 } catch (BadDates e1) {
288                         jLabel5.setText("Last day is before first day in the offer");
289                 } catch (Exception e1) {
290                         e1.printStackTrace();
291                 }
292         }
293
294         private void jButton2_actionPerformed(ActionEvent e) {
295                 this.setVisible(false);
296         }
297
298         private void jTextField3_focusLost() {
299                 try {
300                         new Integer(jTextField3.getText());
301                         jLabel5.setText("");
302                 } catch (NumberFormatException ex) {
303                         jLabel5.setText("Error: Please introduce a number");
304                 }
305         }
306 }